//
//  hyTableViewController.m
//  BaiSiBuDeJie
//
//  Created by uproject on 16/8/29.
//  Copyright © 2016年 tsaievan. All rights reserved.
//

#import "hyTableViewController.h"
#import "BSBaseModel.h"
#import "HyScrollViewModel.h"
#import "HyTableViewCell.h"

@interface hyTableViewController () <UITableViewDataSource,UITableViewDelegate>


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic, strong) BSBaseModel  *baseModel;
@end

@implementation hyTableViewController
- (void)setHyModel:(HyScrollViewModel *)hyModel{
    _hyModel = hyModel;
    [BSBaseModel downDataWithUrl:hyModel.channel_url successBlock:^(BSBaseModel *model) {
        _baseModel = model;
        [self.tableView reloadData];
        
    } failBlock:^(NSError *err) {
        NSLog(@"%@",err);
    }];
}
-(void)addADView
{
    //广告栏
    UIImageView *ADView = [UIImageView new];
    //ADView.contentMode = UIViewContentModeScaleAspectFill;
    ADView.userInteractionEnabled = YES;
    
    self.tableView.tableHeaderView = ADView;
    
    [ADView sd_setImageWithURL:[NSURL URLWithString:@"http://mstatic.spriteapp.cn//ugc/2016/08/18/91EK3H.jpg"]];
    
    ADView.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 64);
    
    
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self addADView];
    //下拉刷新
#pragma mark - 下拉刷新
    //设置刷新的时候展示的图片
    NSArray *idleImages = [NSArray arrayWithObjects:
                           [UIImage imageNamed:@"bdj_mj_refresh_2"],nil];
    
    NSArray *pullingImages = [NSArray arrayWithObject:[UIImage imageNamed:@"bdj_mj_refresh_1"]];
    NSArray *refreshingImages = [NSArray arrayWithObjects:
                                 [UIImage imageNamed:@"bdj_mj_refresh_3"], nil];
    
    
    //创建下拉刷新的实例对象
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [self updata];
        
    }];
    
    //1.设置普通状态的动画图片
    [header setImages:idleImages forState:MJRefreshStateIdle];
    //2.设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [header setImages:pullingImages forState:MJRefreshStatePulling];
    //3.设置正在刷新状态的动画图片
    [header setImages:refreshingImages forState:MJRefreshStateRefreshing];
    
    self.tableView.mj_header = header;
    
#pragma mark --- 自定义刷新状态和刷新时间文字
    // Set title
    [header setTitle:@"下拉可以刷新" forState:MJRefreshStateIdle];
    [header setTitle:@"松开后立即刷新" forState:MJRefreshStatePulling];
    [header setTitle:@"正在刷新 ..." forState:MJRefreshStateRefreshing];
    
    // Set font
    header.stateLabel.font = [UIFont systemFontOfSize:15];
    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
    
    // Set textColor
    header.stateLabel.textColor = [UIColor grayColor];
    header.lastUpdatedTimeLabel.textColor = [UIColor grayColor];
}


#pragma mark - updata

- (void)updata
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:self.hyModel.channel_url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //下载数据成功,停止刷新
        [self endRefresh];
        
        NSDictionary *dic = responseObject;
        
        BSBaseModel *model =  [BSBaseModel mj_objectWithKeyValues:dic];
        
        self.baseModel = model;
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

-(void)endRefresh
{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}



#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.baseModel.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *listArr = self.baseModel.list;
    HyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell"];
    cell.model = listArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

@end
